<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title"></h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>字典管理</cite></a>
        </span>
    </div>


    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="dictionary-search-key">
                <option value="">-请选择-</option>
                <option value="dictType">字典类型</option>
                <option value="dictCode">字典编码</option>
                <option value="dictNameCn">中文名</option>
                <option value="dictNameEn">英文名</option>
            </select>&emsp;
            <input id="dictionary-edit-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="dictionary-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="dictionary-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="dictionary-table" lay-filter="dictionary-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="dictionary-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="show">查看</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        //渲染表格
        table.render({
            elem: '#dictionary-table',
            url: config.base_server + api_user_context + 'dictionary/Dictionary',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: 'ID',width: 80},
                {field: 'dictType', sort: true, title: '字典类型'},
                {field: 'dictCode', sort: true, title: '字典编码'},
                {field: 'dictNameCn', sort: true, title: '中文名'},
                {field: 'dictNameEn', sort: true, title: '英文名'},
                {field: 'sort', sort: true, title: '排序'},
                {align: 'center',fixed: 'right', toolbar: '#dictionary-table-bar', title: '操作',width: 250}
            ]]
        });


        // 工具条点击事件
        table.on('tool(dictionary-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            } else if (obj.event === 'show') {  // 查看
                showEditModel(data,'show');
            }
        });

        // 搜索按钮点击事件
        $('#dictionary-btn-search').click(function () {
            var key = $('#dictionary-search-key').val();
            var value = $('#dictionary-edit-value').val();
            table.reload('dictionary-table', {where: {searchKey: key, searchValue: value}});
        });

        // 添加按钮点击事件
        $('#dictionary-btn-add').click(function () {
            showEditModel();
        });

        //显示表单弹窗
        var showEditModel = function (data,operate) {
            admin.putTempData('t_dictionary', data);
            admin.putTempData('t_operate', operate);
            var title = data ? (operate? '查看字典' : '修改字典') : '添加字典';
            admin.popupCenter({
                title: title,
                path: 'pages/dictionary/dictionary_form.html',
                finish: function () {
                    table.reload('dictionary-table', {});
                }
            });
        };

        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定删除此用户吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req(api_user_context + 'dictionary/Dictionary/'+obj.data.id, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.resp_code == 0) {
                        layer.msg(data.resp_msg, {icon: 1, time: 500});
                        table.reload('dictionary-table', {});
                    } else {
                        layer.msg(data.resp_msg, {icon: 2, time: 500});
                    }
                }, 'DELETE');
            });
        };

    });
</script>